/**   
* @Title: LoadCityList.java 
* @author zengshuai shi   
* @date 2015年8月4日 上午10:41:57 
*/
package com.shanghaionstar.utils;

import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/** 
* @ClassName: LoadCityList 
* @Description: 城市列表
* @author zengshuai shi
* @date 2015年8月4日 上午10:41:57 
*/
public class LoadCityList {
	private InputStream mInputStream;
	List<String[]> listcity;

	public LoadCityList(Context pcontext) {
		try {
			mInputStream = pcontext.getAssets().open("city2.txt");
			listcity = getListArrTxt(mInputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	* @Title: findByEnCity 
	* @Description: 通过英文城市名查找中文城市
	* @param @param city
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	public String findByEnCity(String city) {
		if (city == null || "".equalsIgnoreCase(city)) {
			return "";
		}
		for (String[] s : listcity) {
			if ((city.trim()).equalsIgnoreCase(s[1].trim())) {
				return s[0];
			}
		}
		return city;
	}
	/**
	 * 
	* @Title: findByZhCity 
	* @Description: 通过中文城市名查找英文城市名
	* @param @param city
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	public String findByZhCity(String city) {
		if (city == null || "".equalsIgnoreCase(city)) {
			return "";
		}
		for (String[] s : listcity) {
			if ((city.trim()).equalsIgnoreCase(s[0].trim())) {
				return s[1].trim();
			}
		}
		return city.trim();
	}
	/**
	 * 
	* @Title: getListArrTxt 
	* @Description: 数组
	* @param @param pInputStream
	* @param @return    设定文件 
	* @return List<String[]>    返回类型 
	* @throws
	 */
	public List<String[]> getListArrTxt(InputStream pInputStream) {
		List<String[]> list = new ArrayList<String[]>();
		String str = null;
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(pInputStream, "UTF-8"));
			while ((str = br.readLine()) != null) {
				String[] s1 = str.split(",");
				list.add(s1);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return list;
	}
	
	/**
	 * 
	 * @param 查找中文名的城市
	 * @return
	 */
	public String findZhCity(String city) {
		if (city == null || "".equalsIgnoreCase(city)) {
			return "";
		}
		for (String[] s : listcity) {
			if ((city.trim()).equalsIgnoreCase(s[0].trim()) || (city.trim()).equalsIgnoreCase(s[1].trim())) {
				return s[0];
			}
		}
		return city;
	}
	/**
	 * 
	 * @param 查找英文名的城市
	 * @return
	 */
	public String findEnCity(String city) {
		if (city == null || "".equalsIgnoreCase(city)) {
			return "";
		}
		for (String[] s : listcity) {
			if ((city.trim()).equalsIgnoreCase(s[0].trim()) || (city.trim()).equalsIgnoreCase(s[1].trim())) {
				return s[1];
			}
		}
		return city;
	}
}
